package com.liangjie.cookies;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesWrite extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html");
		for (int i = 0; i < 6; i++) {
			Cookie cookies = new Cookie("First-name:" + i, "First-value" + i);
			resp.addCookie(cookies);
		}

		for (int i = 0; i < 6; i++) {
			Cookie cookies = new Cookie("Second-name" + i, "Second-value" + i);
			cookies.setMaxAge(3600);
			resp.addCookie(cookies);
		}

		resp.getWriter()
				.write("Cookies Writted! <br/> <a href = 'cookiesread'>View cookies</a>");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
